import urllib.request
import urllib.parse

class Bing(object):
    def __init__(self):
        self.url = "http://api.microsofttranslator.com/v2/ajax.svc/TranslateArray2?"
        self.header = {
                'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
                }

    def translate(self, from_lan, to_lan,content,):
        data = {}
        data['from'] = '"' + from_lan + '"'
        data['to'] = '"' + to_lan + '"'
        data['texts'] = '["'
        data['texts'] += content
        data['texts'] += '"]'
        data['options'] = "{}"
        data['oncomplete'] = 'onComplete_3'
        data['onerror'] = 'onError_3'
        data['_'] = '1430745999189'
        data = urllib.parse.urlencode(data).encode('utf-8')
        strUrl = self.url + data.decode() + "&appId=%223DAEE5B978BA031557E739EE1E2A68CB1FAD5909%22"
        res = urllib.request.Request(url=strUrl, headers=self.header)
        response = urllib.request.urlopen(url=res)
        str_data = response.read().decode('utf-8')
        tmp, str_data = str_data.split('"TranslatedText":')
        translate_data = str_data[1:str_data.find('"', 1)]
        return translate_data

class Baidu():
    def __init__(self) -> None:
        self.url = "https://baidu.com/"

    def curl(self):
        res = urllib.request.urlopen(self.url)
        data = res.read().decode('utf-8')
        return data

if __name__ == '__main__':

    content='hello'
    # bing=Bing()
    # print(bing.translate('en', 'zh',content))
    bd = Baidu()
    print(bd.curl())



